package com.guofen.service.food.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.*;
import com.guofen.dto.*;
import com.guofen.pojo.*;
import com.guofen.service.food.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {
    @Autowired
    private FoodTypeMapper foodMapper;
    @Autowired
    private OrderInfoMapper orderMapper;
    @Autowired
    private OrderListMapper foodOrderMapper;

    @Override
    public int addOrder(OrderInfoDTO orderDTO) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        OrderInfo order = new OrderInfo();
        order.setName(orderDTO.getName());
        order.setTime(sdf.format(new Date()));
        order.setStatus("Untreated");

        orderMapper.insertSelective(order);
        order = orderMapper.selectLatest();
        int orderId = order.getId();
        int price = 0;

        List<OrderListDTO> menu = orderDTO.getMenu();
        for (OrderListDTO item : menu) {
            if (item.getNumber() != 0) {
                OrderList foodOrder = new OrderList();
                foodOrder.setFood_name(item.getName());
                foodOrder.setOrder_id(orderId);
                foodOrder.setNumber(item.getNumber());
                foodOrderMapper.insertSelective(foodOrder);

                FoodType food = foodMapper.selectByPrimaryKey(item.getName());
                price += food.getCost() * item.getNumber();
            }
        }

        order.setPrice(price);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int processOrder(int orderId, boolean pass) {
        OrderInfo order = orderMapper.selectByPrimaryKey(orderId);
        if (!pass) {
            order.setStatus("Rejected");
        } else {
            List<OrderList> menu = foodOrderMapper.selectByOrderId(orderId);
            for (OrderList item : menu) {
                FoodType food = foodMapper.selectByPrimaryKey(item.getFood_name());
                food.setStock(food.getStock() - item.getNumber());
                foodMapper.updateByPrimaryKeySelective(food);
            }
            order.setStatus("Processed");
        }
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int modifyOrder(int orderId, List<OrderListDTO> menu) {
        int price = 0;
        foodOrderMapper.deleteByOrderId(orderId);
        for (OrderListDTO item : menu) {
            OrderList foodOrder = new OrderList();
            foodOrder.setOrder_id(orderId);
            foodOrder.setFood_name(item.getName());
            foodOrder.setNumber(item.getNumber());
            foodOrderMapper.insertSelective(foodOrder);

            FoodType food = foodMapper.selectByPrimaryKey(item.getName());
            price += food.getCost() * item.getNumber();
        }
        OrderInfo order = orderMapper.selectByPrimaryKey(orderId);
        order.setPrice(price);
        return orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public Map<String, Object> getOrderByStatus(String[] status, int currentPage, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<OrderInfo> list = orderMapper.selectByStatus(status);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int deleteOrder(int orderId) {
        foodOrderMapper.deleteByOrderId(orderId);
        return orderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public List<OrderList> getMenu(int orderId) {
        return foodOrderMapper.selectByOrderId(orderId);
    }

    @Override
    public List<OrderInfoWithMenu> getCustomerOrder() {
        List<OrderInfoWithMenu> orderWithMenuList = new ArrayList<>();
        String[] status = {"Untreated", "Rejected"};
        List<OrderInfo> orders = orderMapper.selectByStatus(status);
        for (OrderInfo order : orders) {
            List<OrderList> menu = foodOrderMapper.selectByOrderId(order.getId());
            OrderInfoWithMenu orderWithMenu = new OrderInfoWithMenu();
            orderWithMenu.setId(order.getId());
            orderWithMenu.setName(order.getName());
            orderWithMenu.setTime(order.getTime());
            orderWithMenu.setStatus(order.getStatus());
            orderWithMenu.setPrice(order.getPrice());
            orderWithMenu.setMenu(menu);
            orderWithMenuList.add(orderWithMenu);
        }
        return orderWithMenuList;
    }
}
